import random

import numpy as np

t1 = np.array([1, 2, 3, 4, 5])
print(t1)
print(type(t1))

t2 = np.array(range(6))
print(t2)
print(type(t2))

t3 = np.arange(6)
print(t3)
print(type(t3))

# numpy中的数据类型
print(t2.dtype)

# dtype="float32"这种写法也可以
t5 = np.arange(1, 6, dtype=float)
print('{} {} {}'.format(t5, type(t5), t5.dtype))

# 调整数据类型
t6 = t5.astype('int8')
print(t6, end=" ")
print(t6.dtype)

# numpy中的小数
t7 = np.array([random.random() for i in range(10)])
print(t7)
print(t7.dtype)

# 四舍五入取两位小数
t8 = np.round(t7, 2)
print(t8)

# shape和reshape
t9 = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(t9.shape)
t10 = t9.reshape((2, 6))
print(t10, t10.shape)

# flatten，下面两种方式等价
t11 = t10.reshape((t10.shape[0] * t10.shape[1],))
t12 = t10.flatten()
print(t11)
print(t12)

t13 = np.array([1, 2])
t14 = np.array([[1, 2], [3, 4], [5, 6]])
print(t13 * t14)

# 转置
# 以下三种方式等价
t15 = np.arange(24).reshape((4, 6))
print(t15)
print(t15.transpose())
print(t15.T)
print(t15.swapaxes(1, 0))
